<?php

class BigCashesController extends AppController {

    var $name = 'BigCashes';
    var $uses = array('BigCash', 'User', 'Employee', 'Provider', 'BigCashMovement');

    function beforeFilter() {
        parent::beforeFilter();
        // Limito que si es un usuario del tipo user no pueda acceder
        $accion = $this->action;
        if ($this->Auth->user('group_id') != GROUP_ADMIN) {
            $this->Session->setFlash(__('Acceso Restringido', true));
            $this->redirect(array('controller' => 'futures', 'action' => 'index'));
        }
    }
/*
    function index() {
        $this->Cash->recursive = 0;
        $this->set('cashes', $this->paginate());
    }
*/

    function view($id = null) {

        if (!$id) {
            //si no viene el $id de una caja particular, vamos a mostrar la caja actual
            $cash = $this->BigCash->getActual();
            $id = $cash['BigCash']['id'];
        }
        
        $this->BigCash->recursive = -1;
        $this->set('cash', $this->BigCash->read(null, $id));
        
        $filtro = '';
        $fecha_max = '';
        $fecha_min = '';
        $filtro .= "BigCashMovement.big_cash_id = $id";
        
        if (!empty($this->data)) {

            $datos = $this->data['BigCash'];
            
            if (isset($datos['date_from']) && trim($datos['date_from']) != '') {
                list($d, $m, $a) = explode("/", $datos['date_from']);
                $datos['date_from'] = $a . "-" . $m . "-" . $d . ' 00:00:00';

                $filtro .= ' AND BigCashMovement.created >= "' . $datos['date_from'] . '"';
                $fecha_min = 'si';
            }
            if (isset($datos['date_to']) && trim($datos['date_to']) != '') {
                list($d, $m, $a) = explode("/", $datos['date_to']);
                $datos['date_to'] = $a . "-" . $m . "-" . $d . ' 23:59:59';

                $filtro .= ' AND BigCashMovement.created <= "' . $datos['date_to'] . '"';
                $fecha_max = 'si';
            }
            
            if (isset($datos['type']) && trim($datos['type']) != '') {
                $filtro .= " AND BigCashMovement.type = '".$datos['type']."'";
            }
            
            if (isset($datos['amount']) && trim($datos['amount']) != '') {
                $filtro .= " AND BigCashMovement.amount = ".$datos['amount'];
            }
            
            if (isset($datos['movement']) && trim($datos['movement']) != '') {
                $filtro .= " AND BigCashMovement.movement = '".$datos['movement']."'";
            }
            
            if (isset($datos['user_id']) && trim($datos['user_id']) != '') {
                $filtro .= " AND BigCashMovement.user_id = ".$datos['user_id'];
            }
        }

        if ($fecha_max != 'si' || $fecha_min != 'si') {
            //si no cargo alguna de las dos fechas, le voy a poner que vea de 
            //los Últimos 30 días
            $fecha_from = date('Y-m-d 00:00:00', strtotime('-30day'));
            $fecha_to = date('Y-m-d 23:59:59');
            
            $this->data['BigCash']['date_from'] = date('d/m/Y', strtotime($fecha_from));
            $this->data['BigCash']['date_to'] = date('d/m/Y', strtotime($fecha_to));
            
            $filtro .= ' AND BigCashMovement.created >= "' . $fecha_from . '"';
            $filtro .= ' AND BigCashMovement.created <= "' . $fecha_to . '"';
        }
        
        
        $cash_movements = $this->BigCashMovement->find('all', array(
            'recursive' => -1,
            'conditions' => array(
                $filtro
            ),
            'order' => array('BigCashMovement.created' => 'DESC')
            ));
        
        $users = $this->User->find('list');
        $this->set(compact('users', 'cash_movements'));
    }

}
